<template>
    <div class="index-lists">
        <el-card class="!border-none" shadow="never">
            <el-page-header class="mb-4" :content="dramaTitle" @back="$router.back()" />
            <el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true">
                <el-form-item label="剧集标题" prop="title">
                    <el-input class="w-[280px]" clearable v-model="queryParams.title" />
                </el-form-item>

                <el-form-item label="是否付费" prop="is_vip">
                    <el-select class="w-[280px]" v-model="queryParams.is_vip">
                        <el-option label="全部" value="-1" />
                        <el-option label="免费" value="0" />
                        <el-option label="需付费" value="1" />
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none mt-4" shadow="never">
            <div>
                <el-button v-perms="['client:series:add']" type="primary" @click="handleAdd()">
                    <template #icon>
                        <icon name="el-icon-Plus" />
                    </template>
                    新增
                </el-button>
            </div>
            <el-table
                class="mt-4"
                size="large"
                v-loading="pager.loading"
                :data="pager.lists"
            >
                <el-table-column label="剧集编号" prop="series_id" min-width="100" />
                <el-table-column label="短剧标题" prop="title" min-width="100" />
                <el-table-column label="是否付费" prop="is_vip" min-width="100">
                    <template #default="{ row }">
                        <el-tag class="ml-2" :type="row.is_vip ? 'danger' : ''">
                            {{ row.is_vip == 1 ? '需付费' : '免费' }}
                        </el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="序号" prop="sort" min-width="100" />
                <el-table-column label="创建时间" prop="create_time" min-width="100" />
                <el-table-column label="操作" width="120" fixed="right">
                    <template #default="{ row }">
                        <el-button
                            v-perms="['client:series:edit']"
                            type="primary"
                            link
                            @click="handleEdit(row)"
                        >
                            编辑
                        </el-button>
                        <el-button
                            v-perms="['client:series:del']"
                            type="danger"
                            link
                            @click="handleDelete(row.series_id)"
                        >
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup
            v-if="showEdit"
            ref="editRef"
            @success="getLists"
            @close="showEdit = false"
        />
    </div>
</template>
<script lang="ts" setup name="series">
import { seriesDelete, seriesLists } from '@/api/client/series'
import { usePaging } from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
const { query } = useRoute()
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const showEdit = ref(false)
const queryParams = reactive({
    drama_id: String(query.drama_id),
    title: '',
    is_vip: '-1',
})

const dramaTitle = query.drama_title + '【' + query.drama_id +'】'

const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: seriesLists,
    params: queryParams
})


const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add', {drama_id: query.drama_id})
}

const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.getDetail(data)
}

const handleDelete = async (series_id: number) => {
    await feedback.confirm('确定要删除？')
    await seriesDelete({ series_id })
    feedback.msgSuccess('删除成功')
    getLists()
}

onMounted(() => {
    getLists();
});
</script>
